package com.z.xhelper.core.events;

import android.content.ContentValues;

import com.z.xhelper.core.dao.WeChatMsg;
import com.z.xhelper.core.eventbus.IEvent;

// 从微信接收到的消息
public class MsgReceiveEvent extends IEvent<WeChatMsg> {

    public MsgReceiveEvent(ContentValues content) {
        if (content == null) {
            return;
        }
        this.setContext(convertToMessage(content));
    }

    @Override
    public void setContext(WeChatMsg context) {
        this.context = context;
    }

    @Override
    public Class<?> getEventName() {
        return MsgReceiveEvent.class;
    }

    private WeChatMsg convertToMessage(ContentValues contentValues ) {
        WeChatMsg weChatMsg = new WeChatMsg();

        weChatMsg.setTalker(contentValues.getAsString("talker"));
        weChatMsg.setContent(contentValues.getAsString("content"));
        weChatMsg.setType(contentValues.getAsInteger("type"));
        weChatMsg.setCreateTime(contentValues.getAsLong("createTime"));
        weChatMsg.setImgPath(contentValues.getAsString("imgPath"));
        weChatMsg.setIsSend(contentValues.getAsInteger("isSend"));
        weChatMsg.setMsgId(contentValues.getAsLong("msgId"));
        weChatMsg.setStatus(contentValues.getAsInteger("status"));
        weChatMsg.setSendByNickName("");
        // 处理群聊消息
        if (weChatMsg.getTalker().contains("@chatroom")) {
            weChatMsg.setSendByNickName(weChatMsg.getContent().split(":")[0]);
            weChatMsg.setContent(weChatMsg.getContent().split(":")[1].trim());
        }


        return weChatMsg;
    }

}
